package com.tensquare.qahystrix.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * Created by WF on 2020/7/30 14:43
 */
@RestController
@RequestMapping("qa")
public class QAController {
    @Autowired
    private RestTemplate restTemplate;
    private static final String URL = "http://TENSQUARE-BASE";

    @HystrixCommand(fallbackMethod = "helloFail")  //这里的fallbackMathod指调用失败时的方法处理
    @GetMapping("hello/{id}")
    public Result hello(@PathVariable String id){
        return restTemplate.getForObject(URL + "/label/" + id,Result.class );
    }
    //失败的调用方法
    public Result helloFail(String id){
        return new Result(false,StatusCode.ERROR,"熔断器开始工作，调用失败！");
    }
}
